MATLAB Recipes by Michael Paluszek & Stephanie Thomas
Author:Michael Paluszek & Stephanie Thomas
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA
Solution
You have two possibilities for testing the generation of errors in your code: try/catch blocks with assert and the verifyError method available to a TestCase. With warnings, you can either use lastwarn or verifyWarning.
How It Works
A comprehensive set of tests for your code should include all paths, or as close to all paths, as possible; and it must exercise all the warnings and errors that can be generated by your code. You can do this manually by using try/catch blocks to catch errors and comparing the error (MException object) to the expected error. For warnings, you can check lastwarn to see that a warning was issued, like so:
>> lastwarn('');
>> warning('PSS:Book:id','Warning!')
Warning: Warning!
>> [anywarn, anyid] = lastwarn;
>> assert( strcmp (anyid, 'PSS:Book:wrongid'))
Assertion failed.
Here is an example of a try/catch block with assert to detect a specific error:
%% Test that we get the expected error, and pass
errFun = @() error ('PSS:Book:id','Error!');
try
feval (errFun);
catch ME
assert ( strcmp( ME.identifier,'PSS:Book:id'));
end
This test verifies that the error thrown is the one expected; however, it does not detect if no error is thrown at all. For this, you need to add a boolean variable to the try block.
%% This time we don ' t get any error at all
wrongFun = @() disp ('some⊔error-free⊔code.');
tf = false;
try
feval (wrongFun);
tf = true;
catch ME
assert ( strcmp (ME.identifier,'PSS:Book:id'));
end
if (tf)
assert (false,'CatchErrorTest:⊔No⊔error⊔thrown');
end
When you run this code segment, you get the following output:
Some error-free code.
CatchErrorTest: No error thrown
If you run the test as part of a test script with runtests, the test fails.
A far better way to test for warnings and errors is to use the unit test framework’s qualifiers to check that the desired warning or error is generated. Here is an example of verifying a warning, with a test that will pass and a test that will fail; note that you need to pass a function handle to the verifyWarning function.
function tests = WarningsTest
% Create an array of local functions
tests = functiontests (localfunctions);
end
%% Test Functions
function passTest (testCase)
warnFun = @()warning('PSS:Book:id','Warning!');
testCase.verifyWarning(warnFun,'PSS:Book:id');
end
function failTest (testCase)
warnFun = @() warning ('Wrong:id','Warning!');
testCase.verifyWarning (warnFun,'PSS:id','Wrong⊔id');
end
When you run this test function with runtests, you can see that failTest did, in fact, fail.
>> runtests('WarningsTest')
Running WarningsTest
.Warning: Warning!
===========================================================
Verification failed in WarningsTest/failTest.
----------------
Test Diagnostic:
----------------
Wrong id
---------------------
Framework Diagnostic:
---------------------
verifyWarning failed.
--> The function handle did not issue the expected warning.
Actual Warnings:
Wrong:id
Expected Warning:
PSS:id
Evaluated Function:
@()warning('Wrong:id','Warning!')
------------------
Stack Information:
------------------
In /Users/Shared/svn/Manuals/MATLABCookbook/MATLAB/Ch05-Debugging/WarningsTest.m
(failTest) at 12
===========================================================
.
Done WarningsTest
_________
Failure Summary:
Name Failed Incomplete Reason(s)
======================================================================
WarningsTest/failTest X Failed by verification.
Totals:
1 Passed, 1 Failed, 0 Incomplete.
0.047691 seconds testing time.
verifyError works the same way. In practice, you need to make a function handle that includes the inputs that cause the error or warning to be generated.
For advanced programmers, there is a further mechanism for constructing tests using verifyThat with the Constraint class. You can supply your own Diagnostic objects as well. For more information, see the reference pages for these classes along with the Verifiable class.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9924)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9799)
The Mikado Method by Ola Ellnestam Daniel Brolund(9783)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8307)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7787)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7770)
Grails in Action by Glen Smith Peter Ledbrook(7703)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7564)
Windows APT Warfare by Sheng-Hao Ma(6895)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6628)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6496)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6422)
Kotlin in Action by Dmitry Jemerov(5071)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4322)
Functional Programming in JavaScript by Mantyla Dan(4042)
Solidity Programming Essentials by Ritesh Modi(4038)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3829)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3769)
The Ultimate iOS Interview Playbook by Avi Tsadok(3747)
